
#define _COMMON_H_
#define _COURSE_H_
#define _CRT_SECURE_NO_WARNINGS
#define _RECORD_H_
#define _LOG_H_
#define _STUDENT_H_

/*********************************
* 学生选课系统
*********************************/


/*学生信息结构体*/
typedef struct _tStudent {
    char id[128];           /*学号*/
    char name[128];         /*姓名*/
    char password[128];     /*密码*/
}Student, * pStudent;

/*学生信息数组*/
typedef struct _tStudentList {
    int len;            /*数组长度*/
    int capacity;       /*数组容量*/
    pStudent data;      /*数组元素*/
}StudentList, * pStudentList;

/*课程信息结构体*/
typedef struct _tCourse {
    char id[128];       /*编号*/
    char name[128];     /*名称*/
    char type[128];     /*性质*/
    char term[128];     /*学期*/
    int period;         /*学时*/
    double score;       /*学分*/
    int people;         /*选课人数*/
    int limit;          /*人数上限*/
}Course, * pCourse;

/*课程信息数组*/
typedef struct _tCourseList {
    int len;            /*数组长度*/
    int capacity;       /*数组容量*/
    pCourse data;       /*数组元素*/
}CourseList, * pCourseList;

/*选课信息结构体*/
typedef struct _tRecord {
    char student_id[128];   /*学号*/
    char student_name[128]; /*姓名*/
    char course_id[128];    /*课程编号*/
    char course_name[128];  /*课程名称*/
    double socre;           /*学分*/
}Record, * pRecord;

/*选课信息数组*/
typedef struct _tRecordList {
    int len;            /*数组长度*/
    int capacity;       /*数组容量*/
    pRecord data;       /*数组元素*/
}RecordList, * pRecordList;

const char* TYPE_LIST[] = {
    "必修课",
    "选修课"
};

/*得到课程性质*/
const char* selectType() {
    while (1) {
        int len = sizeof(TYPE_LIST) / sizeof(TYPE_LIST[0]);
        printf("\n");
        printf("------------------\n");
        for (int index = 0; index < len; ++index) {
            printf("%-5d %s\n", index + 1, TYPE_LIST[index]);
        }
        printf("------------------\n");
        printf("  请选择课程性质：");
        int option;
        scanf("%d", &option);
        if (option > 0 && option <= len) {
            return TYPE_LIST[option - 1];
        }
    }
    return NULL;
}

/*为课程数组分配空间*/
void allocCourseList(pCourseList list) {
    if (list->capacity == 0) {
        /*首次分配数组空间*/
        list->capacity = 16;
        list->data = (pCourse)malloc(sizeof(Course) * list->capacity);
    } else if ((list->len == list->capacity)) {
        /*为数组空间扩容*/
        list->capacity *= 2;
        list->data = (pCourse)realloc(list->data, sizeof(Course) * list->capacity);
    }
}

/*创建课程数组*/
void createCourseList(pCourseList list) {
    /*清空数组结构体*/
    memset(list, 0U, sizeof(CourseList));
    /*为数组分配内存*/
    allocCourseList(list);
}

/*销毁课程数组*/
void destroyCourseList(pCourseList list) {
    if (list->data) {
        //释放数组内存
        free(list->data);
    }
    /*清空数组结构体*/
    memset(list, 0U, sizeof(CourseList));
}

/*将课程信息添加到数组*/
void addCourseList(pCourseList list, pCourse course) {
    /*分配数组空间*/
    allocCourseList(list);
    list->data[list->len++] = *course;
}

/*从数组中删除课程信息*/
void removeCourseList(pCourseList list, int position) {
    /*保证要删除的元素下标处于合法位置*/
    if (position >= 0 && position < list->len) {
        int index;
        for (index = position + 1; index < list->len; ++index) {
            /*将后续数组元素前移*/
            list->data[index - 1] = list->data[index];
        }
        /*减少数组长度*/
        --list->len;
    }
}

/*通过编号查找数组元素*/
int findCourseListByID(pCourseList list, char* id) {
    int index;
    for (index = 0; index < list->len; ++index) {
        if (strcmp(list->data[index].id, id) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过名称查找数组元素*/
int findCourseListByName(pCourseList list, char* name, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].name, name) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过性质查找数组元素*/
int findCourseListByType(pCourseList list, char* type, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].type, type) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过学分查找数组元素*/
int findCourseListByScore(pCourseList list, double score, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (list->data[index].score == score) {
            return index;
        }
    }
    return -1;
}

/*将课程信息存储到文件*/
void saveCourseFile(const pCourseList list) {
    FILE* output = fopen("courses.txt", "w");
    if (output) {
        int index;
        for (index = 0; index < list->len; ++index) {
            pCourse course = &list->data[index];
            fprintf(output, "%-10s ", course->id);
            fprintf(output, "%-16s ", course->name);
            fprintf(output, "%-16s ", course->type);
            fprintf(output, "%-16s ", course->term);
            fprintf(output, "%-8d ", course->period);
            fprintf(output, "%-8.2lf ", course->score);
            fprintf(output, "%-8d ", course->people);
            fprintf(output, "%-8d ", course->limit);
            fprintf(output, "\n");
        }
        fclose(output);
    } else {
        printf("写文件失败！\n");
    }
}

/*从文件中加载课程信息*/
void loadCourseFile(pCourseList list) {
    FILE* input = fopen("courses.txt", "r");
    if (input) {
        Course course = { 0 };
        list->len = 0;
        while (1) {
            if (fscanf(input, "%s ", course.id) != 1) break;
            if (fscanf(input, "%s ", course.name) != 1) break;
            if (fscanf(input, "%s ", course.type) != 1) break;
            if (fscanf(input, "%s ", course.term) != 1) break;
            if (fscanf(input, "%d ", &course.period) != 1) break;
            if (fscanf(input, "%lf ", &course.score) != 1) break;
            if (fscanf(input, "%d ", &course.people) != 1) break;
            if (fscanf(input, "%d ", &course.limit) != 1) break;
            addCourseList(list, &course);
        }
        fclose(input);
    } else {
        printf("无课程文件读取\n");
    }
}

/*显示课程信息标题*/
void showCourseTitle() {
    printf("%-10s", "编号");
    printf("%-16s", "名称");
    printf("%-16s", "性质");
    printf("%-16s", "学期");
    printf("%-8s", "学时");
    printf("%-8s", "学分");
    printf("%-10s", "已选人数");
    printf("%-10s", "人数上限");
    printf("\n");
}

/*显示课程信息*/
void showCourse(pCourse course) {
    printf("%-10s", course->id);
    printf("%-16s", course->name);
    printf("%-16s", course->type);
    printf("%-16s", course->term);
    printf("%-8d", course->period);
    printf("%-8.2lf", course->score);
    printf("%-10d", course->people);
    printf("%-10d", course->limit);
    printf("\n");
}

/*显示课程信息列表*/
void showCourseList(pCourseList list) {
    int index;
    printf("#显示课程信息#\n");
    showCourseTitle();
    for (index = 0; index < list->len; ++index) {
        pCourse course = &list->data[index];
        showCourse(course);
    }
}

/*编辑课程信息*/
void editCourse(pCourse course) {
    printf("#录入信息#\n");
    if (strlen(course->id)) {
        printf("编号：%s\n", course->id);
    } else {
        printf("编号：");
        scanf("%s", course->id);
    }
    printf("名称：");
    scanf("%s", course->name);
    printf("性质：");
    strcpy(course->type, selectType());
    printf("学期：");
    scanf("%s", course->term);
    printf("学时：");
    scanf("%d", &course->period);
    printf("学分：");
    scanf("%lf", &course->score);
    printf("人数上限：");
    scanf("%d", &course->limit);
}

/*输入课程信息*/
void addCourse(pCourseList list) {
    Course course;
    memset(&course, 0, sizeof(Course));
    printf("#添加课程信息#\n");
    editCourse(&course);
    if (findCourseListByID(list, course.id) == -1) {
        addCourseList(list, &course);
        saveCourseFile(list);
        showCourseTitle();
        showCourse(&course);
        printf("成功添加以上课程信息！\n");
    } else {
        printf("该编号已存在，添加失败！\n");
    }
}

/*修改课程信息*/
void updateCourse(pCourseList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#修改课程信息#\n");
    printf("输入课程编号：");
    scanf("%s", id);
    position = findCourseListByID(list, id);
    if (position != -1) {
        editCourse(&list->data[position]);
        saveCourseFile(list);
        showCourseTitle();
        showCourse(&list->data[position]);
        printf("成功修改以上课程信息！\n");
    } else {
        printf("未找到该课程!\n");
    }
}

/*删除课程信息*/
void removeCourse(pCourseList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#删除课程信息#\n");
    printf("输入课程编号：");
    scanf("%s", id);
    position = findCourseListByID(list, id);
    if (position != -1) {
        showCourseTitle();
        showCourse(&list->data[position]);
        removeCourseList(list, position);
        saveCourseFile(list);
        printf("成功删除以上课程信息!\n");
    } else {
        printf("未找到该课程!\n");
    }
}

/*按编号查询*/
void searchCourseByID(pCourseList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#按编号查询#\n");
    printf("输入课程编号：");
    scanf("%s", id);
    position = findCourseListByID(list, id);
    if (position != -1) {
        showCourseTitle();
        showCourse(&list->data[position]);
    } else {
        printf("未找到该课程!\n");
    }
}

/*按名称查询*/
void searchCourseByName(pCourseList list) {
    char name[128] = { 0 };
    int position = -1;
    printf("#按编号查询#\n");
    printf("输入课程名称：");
    scanf("%s", name);
    position = findCourseListByName(list, name, 0);
    if (position != -1) {
        showCourseTitle();
        do {
            showCourse(&list->data[position]);
            position = findCourseListByName(list, name, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该课程!\n");
    }
}

/*按性质查询*/
void searchCourseByType(pCourseList list) {
    char type[128] = { 0 };
    int position = -1;
    printf("#按编号查询#\n");
    printf("输入课程性质：");
    scanf("%s", type);
    position = findCourseListByType(list, type, 0);
    if (position != -1) {
        showCourseTitle();
        do {
            showCourse(&list->data[position]);
            position = findCourseListByType(list, type, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该课程!\n");
    }
}

/*按学分查询*/
void searchCourseByScore(pCourseList list) {
    double score = { 0 };
    int position = -1;
    printf("#按编号查询#\n");
    printf("输入课程学分：");
    scanf("%lf", &score);
    position = findCourseListByScore(list, score, 0);
    if (position != -1) {
        showCourseTitle();
        do {
            showCourse(&list->data[position]);
            position = findCourseListByScore(list, score, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该课程!\n");
    }
}

/*查询课程信息*/
void searchCourse(pCourseList list) {
    while (1) {
        int option;
        printf("#查询课程信息#\n");
        printf(" 1 按编号查询\n");
        printf(" 2 按名称查询\n");
        printf(" 3 按性质查询\n");
        printf(" 4 按学分查询\n");
        printf(" 0 返回\n");
        scanf("%d", &option);
        if (option == 0) break;
        switch (option) {
        case 1:
            searchCourseByID(list);
            break;
        case 2:
            searchCourseByName(list);
            break;
        case 3:
            searchCourseByType(list);
            break;
        case 4:
            searchCourseByScore(list);
            break;
        }
    }
}


/*日期时间格式化*/
void formatDateTime(time_t tt, char* text) {
    struct tm* t;
    t = localtime(&tt);
    sprintf(text, "[%04d-%02d-%02d#%02d:%02d:%02d]", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}

/*添加日志*/
void addLog(const char* id, const char* name, const char* content) {
    FILE* output = fopen("log.txt", "a");
    if (output) {
        char timestamp[256] = { 0 };
        formatDateTime(time(NULL), timestamp);
        fprintf(output, "%s %-16s %-16s %s\n", timestamp, id, name, content);
        fclose(output);
    } else {
        printf("读文件失败！\n");
    }
}

/*加载日志*/
void loadLog() {
    FILE* input = fopen("log.txt", "r");
    printf("╔----------------------------╗\n");
    printf("       $ 日志 $\n");
    if (input) {
        char line[1024] = { 0 };
        while (fgets(line, sizeof(line), input)) {
            printf("%s", line);
        }
        fclose(input);
    }
    printf("╚----------------------------╝\n");
}


/*为选课数组分配空间*/
void allocRecordList(pRecordList list) {
    if (list->capacity == 0) {
        /*首次分配数组空间*/
        list->capacity = 16;
        list->data = (pRecord)malloc(sizeof(Record) * list->capacity);
    } else if ((list->len == list->capacity)) {
        /*为数组空间扩容*/
        list->capacity *= 2;
        list->data = (pRecord)realloc(list->data, sizeof(Record) * list->capacity);
    }
}

/*创建选课数组*/
void createRecordList(pRecordList list) {
    /*清空数组结构体*/
    memset(list, 0U, sizeof(RecordList));
    /*为数组分配内存*/
    allocRecordList(list);
}

/*销毁选课数组*/
void destroyRecordList(pRecordList list) {
    if (list->data) {
        //释放数组内存
        free(list->data);
    }
    /*清空数组结构体*/
    memset(list, 0U, sizeof(RecordList));
}

/*将选课信息添加到数组*/
void addRecordList(pRecordList list, pRecord record) {
    /*分配数组空间*/
    allocRecordList(list);
    list->data[list->len++] = *record;
}

/*从数组中删除选课信息*/
void removeRecordList(pRecordList list, int position) {
    /*保证要删除的元素下标处于合法位置*/
    if (position >= 0 && position < list->len) {
        int index;
        for (index = position + 1; index < list->len; ++index) {
            /*将后续数组元素前移*/
            list->data[index - 1] = list->data[index];
        }
        /*减少数组长度*/
        --list->len;
    }
}

/*通过学号查找数组元素*/
int findRecordListByStudentID(pRecordList list, char* id, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].student_id, id) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过课程编号查找数组元素*/
int findRecordListByCourseID(pRecordList list, char* id, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].course_id, id) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过学号查找数组元素*/
int findRecordListByStudentIDCourseID(pRecordList list, char* student_id, char* course_id) {
    int index;
    for (index = 0; index < list->len; ++index) {
        if (strcmp(list->data[index].student_id, student_id) == 0 && strcmp(list->data[index].course_id, course_id) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过学生学号统计学分*/
double scoreRecordListByStudentID(pRecordList list, char* id) {
    double score = 0;
    int index;
    for (index = 0; index < list->len; ++index) {
        pRecord record = &list->data[index];
        if (strcmp(record->student_id, id) == 0) {
            score += record->socre;
        }
    }
    return score;
}

/*将选课信息存储到文件*/
void saveRecordFile(const pRecordList list) {
    FILE* output = fopen("records.txt", "w");
    if (output) {
        int index;
        for (index = 0; index < list->len; ++index) {
            pRecord record = &list->data[index];
            fprintf(output, "%-10s ", record->student_id);
            fprintf(output, "%-16s ", record->student_name);
            fprintf(output, "%-16s ", record->course_id);
            fprintf(output, "%-16s ", record->course_name);
            fprintf(output, "%-8.2lf ", record->socre);
            fprintf(output, "\n");
        }
        fclose(output);
    } else {
        printf("写文件失败！\n");
    }
}

/*从文件中加载选课信息*/
void loadRecordFile(pRecordList list) {
    FILE* input = fopen("records.txt", "r");
    if (input) {
        Record record = { 0 };
        list->len = 0;
        while (1) {
            if (fscanf(input, "%s ", record.student_id) != 1) break;
            if (fscanf(input, "%s ", record.course_name) != 1) break;
            if (fscanf(input, "%s ", record.course_id) != 1) break;
            if (fscanf(input, "%s ", record.course_name) != 1) break;
            if (fscanf(input, "%lf ", &record.socre) != 1) break;
            addRecordList(list, &record);
        }
        fclose(input);
    } else {
        printf("无选课文件读取！\n");
    }
}

/*显示选课信息标题*/
void showRecordTitle() {
    printf("%-10s", "学号");
    printf("%-16s", "姓名");
    printf("%-16s", "课程编号");
    printf("%-16s", "课程名称");
    printf("%-8s", "学分");
    printf("\n");
}

/*显示选课信息*/
void showRecord(pRecord record) {
    printf("%-10s", record->student_id);
    printf("%-16s", record->student_name);
    printf("%-16s", record->course_id);
    printf("%-16s", record->course_name);
    printf("%-8.2lf", record->socre);
    printf("\n");
}

/*显示选课信息列表*/
void showRecordList(pRecordList list, char* id) {
    int index;
    printf("#显示选课信息#\n");
    showRecordTitle();
    for (index = 0; index < list->len; ++index) {
        pRecord record = &list->data[index];
        if (id == NULL || strcmp(record->student_id, id) == 0) {
            showRecord(record);
        }
    }
    if (strlen(id)) {
        double score = scoreRecordListByStudentID(list, id);
        printf("----------\n");
        printf("已选课程总学分：%.2lf\n", score);
    }
}

/*按学生学号查询选课信息*/
void searchRecordByStudentID(pRecordList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#按学号查询#\n");
    printf("输入学生学号：");
    scanf("%s", id);
    position = findRecordListByStudentID(list, id, 0);
    if (position != -1) {
        showRecordTitle();
        do {
            showRecord(&list->data[position]);
            position = findRecordListByStudentID(list, id, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该选课!\n");
    }
}

/*按课程编号查询选课信息*/
void searchRecordByCourseID(pRecordList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#按课程编号查询#\n");
    printf("输入课程编号：");
    scanf("%s", id);
    position = findRecordListByCourseID(list, id, 0);
    if (position != -1) {
        showRecordTitle();
        do {
            showRecord(&list->data[position]);
            position = findRecordListByCourseID(list, id, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该选课!\n");
    }
}

/*查询选课信息*/
void searchRecord(pRecordList list) {
    while (1) {
        int option;
        printf("#查询选课信息#\n");
        printf(" 1 按学生学号查询\n");
        printf(" 2 按课程编号查询\n");
        printf(" 0 返回\n");
        scanf("%d", &option);
        if (option == 0) break;
        switch (option) {
        case 1:
            searchRecordByStudentID(list);
            break;
        case 2:
            searchRecordByCourseID(list);
            break;
        }
    }
}


/*为学生数组分配空间*/
void allocStudentList(pStudentList list) {
    if (list->capacity == 0) {
        /*首次分配数组空间*/
        list->capacity = 16;
        list->data = (pStudent)malloc(sizeof(Student) * list->capacity);
    } else if ((list->len == list->capacity)) {
        /*为数组空间扩容*/
        list->capacity *= 2;
        list->data = (pStudent)realloc(list->data, sizeof(Student) * list->capacity);
    }
}

/*创建学生数组*/
void createStudentList(pStudentList list) {
    /*清空数组结构体*/
    memset(list, 0U, sizeof(StudentList));
    /*为数组分配内存*/
    allocStudentList(list);
}

/*销毁学生数组*/
void destroyStudentList(pStudentList list) {
    if (list->data) {
        //释放数组内存
        free(list->data);
    }
    /*清空数组结构体*/
    memset(list, 0U, sizeof(StudentList));
}

/*将学生信息添加到数组*/
void addStudentList(pStudentList list, pStudent student) {
    /*分配数组空间*/
    allocStudentList(list);
    list->data[list->len++] = *student;
}

/*从数组中删除学生信息*/
void removeStudentList(pStudentList list, int position) {
    /*保证要删除的元素下标处于合法位置*/
    if (position >= 0 && position < list->len) {
        int index;
        for (index = position + 1; index < list->len; ++index) {
            /*将后续数组元素前移*/
            list->data[index - 1] = list->data[index];
        }
        /*减少数组长度*/
        --list->len;
    }
}

/*通过学号查找数组元素*/
int findStudentListByID(pStudentList list, char* id, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].id, id) == 0) {
            return index;
        }
    }
    return -1;
}

/*通过姓名查找数组元素*/
int findStudentListByName(pStudentList list, char* name, int begin) {
    int index;
    for (index = begin; index < list->len; ++index) {
        if (strcmp(list->data[index].name, name) == 0) {
            return index;
        }
    }
    return -1;
}

/*将学生信息存储到文件*/
void saveStudentFile(const pStudentList list) {
    FILE* output = fopen("students.txt", "w");
    if (output) {
        int index;
        for (index = 0; index < list->len; ++index) {
            pStudent student = &list->data[index];
            fprintf(output, "%-10s ", student->id);
            fprintf(output, "%-16s ", student->name);
            fprintf(output, "%-16s ", student->password);
            fprintf(output, "\n");
        }
        fclose(output);
    } else {
        printf("写文件失败！\n");
    }
}

/*从文件中加载学生信息*/
void loadStudentFile(pStudentList list) {
    FILE* input = fopen("students.txt", "r");
    if (input) {
        Student student = { 0 };
        list->len = 0;
        while (1) {
            if (fscanf(input, "%s ", student.id) != 1) break;
            if (fscanf(input, "%s ", student.name) != 1) break;
            if (fscanf(input, "%s ", student.password) != 1) break;
            addStudentList(list, &student);
        }
        fclose(input);
    } else {
        printf("无学生文件读取！\n");
    }
}

/*显示学生信息标题*/
void showStudentTitle() {
    printf("%-10s", "学号");
    printf("%-16s", "姓名");
    printf("%-16s", "密码");
    printf("\n");
}

/*显示学生信息*/
void showStudent(pStudent student) {
    printf("%-10s", student->id);
    printf("%-16s", student->name);
    printf("%-16s", student->password);
    printf("\n");
}

/*显示学生信息列表*/
void showStudentList(pStudentList list) {
    int index;
    printf("#显示学生信息#\n");
    showStudentTitle();
    for (index = 0; index < list->len; ++index) {
        pStudent student = &list->data[index];
        showStudent(student);
    }
}

/*编辑学生信息*/
void editStudent(pStudent student) {
    printf("#录入信息#\n");
    if (strlen(student->id)) {
        printf("学号：%s\n", student->id);
    } else {
        printf("学号：");
        scanf("%s", student->id);
    }
    printf("姓名：");
    scanf("%s", student->name);
    printf("密码：");
    scanf("%s", student->password);
}

/*输入学生信息*/
void addStudent(pStudentList list) {
    Student student;
    memset(&student, 0, sizeof(Student));
    printf("#添加学生信息#\n");
    editStudent(&student);
    if (findStudentListByID(list, student.id, 0) == -1) {
        addStudentList(list, &student);
        saveStudentFile(list);
        showStudentTitle();
        showStudent(&student);
        printf("成功添加以上学生信息！\n");
    } else {
        printf("该学号已存在，添加失败！\n");
    }

}

/*修改学生信息*/
void updateStudent(pStudentList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#修改学生信息#\n");
    printf("输入学生学号：");
    scanf("%s", id);
    position = findStudentListByID(list, id, 0);
    if (position != -1) {
        editStudent(&list->data[position]);
        saveStudentFile(list);
        showStudentTitle();
        showStudent(&list->data[position]);
        printf("成功修改以上学生信息！\n");
    } else {
        printf("未找到该学生!\n");
    }

}

/*删除学生信息*/
void removeStudent(pStudentList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#删除学生信息#\n");
    printf("输入学生学号：");
    scanf("%s", id);
    position = findStudentListByID(list, id, 0);
    if (position != -1) {
        showStudentTitle();
        showStudent(&list->data[position]);
        removeStudentList(list, position);
        saveStudentFile(list);
        printf("成功删除以上学生信息!\n");
    } else {
        printf("未找到该学生!\n");
    }

}

/*按学号查询*/
void searchStudentByID(pStudentList list) {
    char id[128] = { 0 };
    int position = -1;
    printf("#按学号查询#\n");
    printf("输入学生学号：");
    scanf("%s", id);
    position = findStudentListByID(list, id, 0);
    if (position != -1) {
        showStudentTitle();
        do {
            showStudent(&list->data[position]);
            position = findStudentListByID(list, id, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该学生!\n");
    }

}

/*按姓名查询*/
void searchStudentByName(pStudentList list) {
    char name[128] = { 0 };
    int position = -1;
    printf("#按学号查询#\n");
    printf("输入学生姓名：");
    scanf("%s", name);
    position = findStudentListByName(list, name, 0);
    if (position != -1) {
        showStudentTitle();
        do {
            showStudent(&list->data[position]);
            position = findStudentListByName(list, name, position + 1);
        } while (position != -1);
    } else {
        printf("未找到该学生!\n");
    }

}

/*查询学生信息*/
void searchStudent(pStudentList list) {
    while (1) {
        int option;
        printf("#查询学生信息#\n");
        printf(" 1 按学号查询\n");
        printf(" 2 按姓名查询\n");
        printf(" 0 返回\n");
        scanf("%d", &option);
        if (option == 0) break;
        switch (option) {
        case 1:
            searchStudentByID(list);
            break;
        case 2:
            searchStudentByName(list);
            break;
        }
    }
}

/*输入密码*/
void inputPassword(char password[], int capacity) {
    scanf("%s", password);
}

/*管理员登录菜单*/
int loginAdmin() {
    char id[128] = { 0 };
    char password[128] = { 0 };
    printf("╔----------------------------╗\n");
    printf("       $ 登录 $\n");
    printf("     账号 : ");
    scanf("%s", id);
    printf("     密码 : ");
    inputPassword(password, 16);
    printf("╚----------------------------╝\n");
    if (strcmp(id, "admin") == 0 && strcmp(password, "123") == 0) {
        return 1;
    } else {
        printf("\n登录失败，户名或密码错误！\n");

    }
    return 0;
}

/*学生登录菜单*/
pStudent loginStudent(pStudentList list) {
    char id[128] = { 0 };
    char password[128] = { 0 };
    int position = -1;
    printf("╔----------------------------╗\n");
    printf("       $ 登录 $\n");
    printf("     学号 : ");
    scanf("%s", id);
    printf("     密码 : ");
    inputPassword(password, 16);
    printf("╚----------------------------╝\n");
    position = findStudentListByID(list, id, 0);
    if (position != -1) {
        pStudent student = &list->data[position];
        if (strcmp(student->password, password) == 0) {
            return student;
        }
    }
    printf("\n登录失败，户名或密码错误！\n");

    return NULL;
}


